<template>
  <div>
    <h3>家庭成员基本信息名单</h3>
    <!--卡片视图-->
    <el-card>
      <!--搜索与添加区域-->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入身份编号"
                    v-model="quaryInfo.searchText" clearable @clear="getStudentList">
            <el-button slot="append" icon="el-icon-search" @click="getStudentList">搜索</el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisable =
                true"> 添加家庭成员
          </el-button>
        </el-col>
      </el-row>

      <!--用户列表区-->
      <el-table :data="stulist" border stripe>
        <el-table-column label="身份编号" type="index"></el-table-column>
        <el-table-column label="考号" prop="Testnum"></el-table-column>
        <el-table-column label="姓名" prop="Name"></el-table-column>
        <el-table-column label="性别" prop="Sex"></el-table-column>
        <el-table-column label="年龄" prop="Age"></el-table-column>
        <el-table-column label="出生日期" prop="Politic"></el-table-column>
        <el-table-column label="配偶" prop="Status"></el-table-column>
        <el-table-column label="荣誉" prop="Education"></el-table-column>
        <el-table-column label="孩子" prop="Source"></el-table-column>
        <el-table-column label="父亲" prop="Aimmajor"></el-table-column>
        <el-table-column label="母亲" prop="Aimclass"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--修改按钮-->
            <el-button type="primary" icon="el-icon-edit" size="mini"
                       @click="showEditDialog(scope.row.Testnum)"></el-button>
            <!--删除按钮-->
            <el-button type="danger" icon="el-icon-delete" size="mini"
            @click="removStuById(scope.row.Testnum)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page.sync="quaryInfo.page" :page-sizes="[8, 10,20]" :page-size="quaryInfo.limit"
                     layout="total, sizes, prev, pager, next" :total="total">
      </el-pagination>
    </el-card>

    <!--添加学生对话框-->
    <el-dialog title="添加学生" :visible.sync="addDialogVisable" width="50%" @close
      ="addDiaogClose">
      <!--内容主体区域-->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">

        <el-form-item label="身份编号" prop="testnum">
          <el-input v-model="addForm.testnum"></el-input>
        </el-form-item>


        <el-form-item label="姓名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-input v-model="addForm.sex"></el-input>
        </el-form-item>

        <el-form-item label="年龄" prop="age">
          <el-input v-model="addForm.age"></el-input>
        </el-form-item>

        <el-form-item label="出生日期" prop="politic">
          <el-input v-model="addForm.politic"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-input v-model="addForm.status"></el-input>
        </el-form-item>

        <el-form-item label="配偶" prop="education">
          <el-input v-model="addForm.education"></el-input>
        </el-form-item>

        <el-form-item label="孩子" prop="source">
          <el-input v-model="addForm.source"></el-input>
        </el-form-item>

        <el-form-item label="父亲" prop="aimmajor">
          <el-input v-model="addForm.aimmajor"></el-input>
        </el-form-item>

        <el-form-item label="母亲" prop="aimclass">
          <el-input v-model="addForm.aimclass"></el-input>
        </el-form-item>
      </el-form>
      <!--底部-->
      <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisable = false">取 消</el-button>
    <el-button type="primary" @click="addStu">确 定</el-button>
  </span>
    </el-dialog>

    <!--修改学生-->
    <el-dialog title="修改信息" :visible.sync="editdialogVisible" width="50%"
               @close="editDialogClosed">
      <el-form :model="editForm" ref="editFormRef" label-width="70px">

        <el-form-item label="身份编号" prop="Testnum">
          <el-input v-model="editForm.Testnum" disabled></el-input>
        </el-form-item>

        <el-form-item label="姓名" prop="Name">
          <el-input v-model="editForm.Name"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="Sex">
          <el-input v-model="editForm.Sex"></el-input>
        </el-form-item>

        <el-form-item label="年龄" prop="Age">
          <el-input v-model="editForm.Age"></el-input>
        </el-form-item>


        <el-form-item label="出生日期" prop="Politic">
          <el-input v-model="editForm.Politic"></el-input>
        </el-form-item>

        <el-form-item label="配偶" prop="Status">
          <el-input v-model="editForm.Status"></el-input>
        </el-form-item>


        <el-form-item label="荣誉" prop="Education">
          <el-input v-model="editForm.Education"></el-input>
        </el-form-item>


        <el-form-item label="孩子" prop="Source">
          <el-input v-model="editForm.Source"></el-input>
        </el-form-item>

        <el-form-item label="父亲" prop="Aimmajor">
          <el-input v-model="editForm.Aimmajor"></el-input>
        </el-form-item>


        <el-form-item label="母亲" prop="Aimclass">
          <el-input v-model="editForm.Aimclass"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="editdialogVisible = false">取 消</el-button>
       <el-button type="primary" @click="editStuInfo">确 定</el-button>
       </span>
    </el-dialog>
  </div>
</template>


<script>
export default {
  data() {
    return {
      //获取用户列表的参数对象
      quaryInfo: {
        searchText: '',
        page: 1,
        //当前每页显示多少条数据
        limit: 8
      },
      stulist: [],
      total: 0,
      //控制显示与隐藏
      addDialogVisable: false,
      //添加用户的表单数据
      addForm: {
        testnum: '',
        name: '',
        sex: '',
        age: '',
        politic: '',
        status: '',
        education: '',
        source: '',
        aimmajor: '',
        aimclass: '',
      },
      addFormRules: {
        testnum: [
          {
            required: true, message: "身份编号不能为空",
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true, message: "姓名不能为空",
            trigger: 'blur'
          }
        ],
        sex: [
          {
            required: true, message: "性别不能为空",
            trigger: 'blur'
          }
        ],
        age: [
          {
            required: true, message: "年龄不能为空",
            trigger: 'blur'
          }
        ],
        politic: [
          {
            required: true, message: " ",
            trigger: 'blur'
          }
        ],
        status: [
          {
            required: true, message: " ",
            trigger: 'blur'
          }
        ],
        education: [
          {
            required: true, message: " ",
            trigger: 'blur'
          }
        ],
        source: [
          {
            required: true, message: " ",
            trigger: 'blur'
          }
        ],
        aimmajor: [
          {
            required: true, message: " ",
            trigger: 'blur'
          }
        ],
        aimclass: [
          {
            required: true, message: " ",
            trigger: 'blur'
          }
        ],
      },
      //控制修改对话框的隐藏
      editdialogVisible: false,
      //查询到的用户信息对象
      editForm: {}
    }
  },

  created() {
    this.getStudentList()
  },

  methods: {
    async getStudentList() {
      const {data: res} = await this.$http.get('stuinfo', {params: this.quaryInfo})
      if (res.code !== 20000) {
        return this.$message.error('获取失败')
      }
      this.stulist = res.data.value
      this.total = res.data.total
      console.log(res)
    },
    //监听pagesize改变的事件
    handleSizeChange(newLimit) {
      // console.log(newSize)
      this.quaryInfo.limit = newLimit
      this.getStudentList()
    },
    //监听页码值改变的时间
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.getStudentList()
    },
    //监听添加对话框的事件
    addDiaogClose() {
      this.$refs.addFormRef.resetFields()
    },
    //点击按钮添加学生
    addStu() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        //真正的添加用户的网络请求
        const {data: res} = await this.$http.post('stuinfo', this.addForm)
        if (res.code !== 20000) {
          this.$message.error('添加失败')
        }
        this.$message.success('添加用户成功')
        this.addDialogVisable = false
        //重新获取用户列表
        this.getStudentList()
      })
    },
    //展示编辑用户的对话框
    async showEditDialog(id) {
      this.quaryInfo.searchText = id
      const {data: res} = await this.$http.get('stuinfo', {params: this.quaryInfo})
      this.editForm = res.data.value[0]
      this.editdialogVisible = true
    },
    //监听修改用户对话框关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    editStuInfo() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return console.log(valid)
        const {data: res} = await this.$http.put('stuinfo', {
            Name: this.editForm.Name,
            Sex: this.editForm.Sex,
            Age: this.editForm.Age,
            Politic: this.editForm.Politic,
            Status: this.editForm.Status,
            Education: this.editForm.Education,
            Source: this.editForm.Source,
            Aimmajor: this.editForm.Aimmajor,
            Aimclass: this.editForm.Aimclass,
        })
        this.editdialogVisible = false
        this.getStudentList()
       })
      },
    async removStuById(id) {
        const  confirmResult = await this.$confirm('此操作将永久删除该成员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      const {data: res} = await this.$http.delete('stuinfo/' + id)
      this.$message.success('删除用户成功')
      this.getStudentList()
    }
    },
  }
</script>

<style lang="less" scoped>

</style>
